using System.Collections.Generic;
using System.Linq;
using Provausio.Common.Objects;
using TrainingManagement.api.Models.Data.CRUD;
using TrainingManagement.api.Models.Data.CRUD.Employee;

namespace TrainingManagement.api.Models.BusinessLogic.Employee
{
    public class EmployeeSelectOperation : BusinessOperation<int?, List<BusinessObjects.Employee>>
    {
        readonly EmployeeSelectDataSource _employeeSelectDataSource = new EmployeeSelectDataSource();

        public override List<BusinessObjects.Employee> Run(int? input)
        {
            var emps = _employeeSelectDataSource.Execute(input);
            foreach (var emp in emps.Where(emp => emp.ReportsToId != null))
            {
                emp.ReportsTo = _employeeSelectDataSource.Execute(emp.ReportsToId).FirstOrDefault();
            }
            return emps;
        }
    }
}